package com.green.javagreen.service;


import com.green.javagreen.bean.AdminUserBean;
import com.green.javagreen.util.ExampleUtil;
import com.green.javagreen.util.IntegerUtil;
import com.green.javagreen.util.TextUtil;
import com.green.javagreen.util.UpdateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

/**
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    private AdminUserRepository adminUserRepository;

    @Autowired
    public void setAdminUserRepository(AdminUserRepository adminUserRepository) {
        this.adminUserRepository = adminUserRepository;
    }

    @Override
    public AdminUserBean save(AdminUserBean bean) {
        AdminUserBean exitBean = get(bean.getId());
        if (exitBean == null) {
            exitBean = bean;
        } else {
            UpdateTool.copyNullProperties(bean, exitBean);
        }
        adminUserRepository.save(exitBean);
        return exitBean;
    }

    @Override
    public AdminUserBean get(Integer id) {
        if (IntegerUtil.isNotValid(id)) {
            return null;
        }
        return adminUserRepository.findById(id).orElse(null);
    }

    @Override
    public AdminUserBean get(AdminUserBean bean) {
        return adminUserRepository.findOne(ExampleUtil.getSimpleExample(bean)).orElse(null);
    }

    @Override
    public Page<AdminUserBean> find(AdminUserBean bean) {
        return adminUserRepository.findAll(ExampleUtil.getSimpleExample(bean), bean.getPageRequest());
    }

    @Override
    public void delete(Integer id) {
        if (IntegerUtil.isNotValid(id)) {
            return;
        }
        adminUserRepository.deleteById(id);
    }

    @Override
    public AdminUserBean findByCode(String code) {
        if (TextUtil.isNotValid(code)) {
            return null;
        }
        return adminUserRepository.findByCodeEquals(code);
    }
}
